package org.seven.cpcrkapp.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import com.andreabaccega.widget.FormEditText;

import org.seven.cpcrkapp.R;
import org.seven.cpcrkapp.model.Result;
import org.seven.cpcrkapp.util.DialogUtils;
import org.seven.cpcrkapp.util.HttpUtils;
import org.seven.cpcrkapp.util.WebAppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {
    @BindView(R.id.toolbar_setting)
    Toolbar toolbar;
    @BindView((R.id.setting_ok))
    Button b_ok;
    @BindView(R.id.t_ip)
    FormEditText t_ip;
    @BindView(R.id.t_port)
    FormEditText t_port;
    @BindView(R.id.t_name)
    FormEditText t_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        t_ip.setText(WebAppUtils.getIp());
        t_port.setText(WebAppUtils.getPort());
        t_name.setText(WebAppUtils.getName());
    }

    @OnClick(R.id.setting_ok)
    void onClickOk(View v) {
        FormEditText[] allFields = {t_ip, t_port, t_name};
        boolean allValid = true;
        for (FormEditText field : allFields) {
            allValid = field.testValidity() && allValid;
        }
        if (allValid) {
            final ProgressDialog pd = DialogUtils.createProgressDialog(SettingActivity.this);
            pd.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        WebAppUtils.save(t_ip.getText().toString(), t_port.getText().toString(), t_name.getText().toString());
                        final Result result = HttpUtils.getResult("/app/core/test");
                        pd.dismiss();
                        runOnUiThread(new Runnable() {
                            public void run() {
                                DialogUtils.alert(result.getMsg());
                            }
                        });
                    } catch (Exception e) {
                        WebAppUtils.clear();
                        pd.dismiss();
                        final String msg = e.getMessage();
                        runOnUiThread(new Runnable() {
                            public void run() {
                                DialogUtils.alert(msg);
                            }
                        });
                    }
                }
            }).start();
        }
    }
}
